<?php

/**
 * portfolio actions.
 *
 * @package    lezgro
 * @subpackage portfolio
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class portfolioActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $solution_id=$request->getParameter('solution_id');
    if($solution_id!=null&&$solution_id!=''){
    $this->portfolios=Doctrine::getTable('Portfolio')->getPortfoliosBySolutionId($solution_id);
    $this->solution_id=$solution_id;
    $this->solution=Doctrine::getTable('Solution')->findOneById($solution_id);
    }else{
        $this->portfolios = Doctrine_Core::getTable('Portfolio')
      ->createQuery('a')
      ->execute();
    }
  }

 
  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PortfolioForm();
    $this->form->setDefault('solution_id', $request->getParameter('solution_id'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PortfolioForm();

    $portfolio=$this->processForm($request, $this->form);
    $forma=($request->getParameter($this->form->getName()));
     $this->redirect('portfolio/edit?id='.$portfolio->getId());
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($portfolio = Doctrine_Core::getTable('Portfolio')->find(array($request->getParameter('id'))), sprintf('Object portfolio does not exist (%s).', $request->getParameter('id')));
    $this->form = new PortfolioForm($portfolio);
    $this->portfolio=$portfolio;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($portfolio = Doctrine_Core::getTable('Portfolio')->find(array($request->getParameter('id'))), sprintf('Object portfolio does not exist (%s).', $request->getParameter('id')));
    $this->form = new PortfolioForm($portfolio);

    $this->portfolio=$this->processForm($request, $this->form);
    if(isset($this->portfolio))
        $this->redirect('portfolio');
    //$this->redirect('portfolio/edit?id='.$portfolio->getId());

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($portfolio = Doctrine_Core::getTable('Portfolio')->find(array($request->getParameter('id'))), sprintf('Object portfolio does not exist (%s).', $request->getParameter('id')));
    $portfolio->delete();

    $this->redirect('portfolio/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $portfolio = $form->save();

     return $portfolio;}
  }
   public function executeGenerateFriendlyUri(sfWebRequest $request){
      $this->forward404Unless($request->isXmlHttpRequest(), sprintf('Error there no such uri'));
      $text=$request->getParameter('text');
      $slug=UrlParser::slugify($text);
      if(!Doctrine::getTable('Portfolio')->findOneBy('slug',$slug))
          $response['slug']=$slug;
      else $response['errors']=array('Error'=>'Change uri manually, this alredy in use');
      return $this->renderText(json_encode($response));
  }
}
